﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChoDiaOc_HK_WPF.Controls;
using ChoDiaOc_HK_WPF.Models;

namespace ChoDiaOc_HK_WPF.Views.LapBaoBieu
{
    class TKKhachHang
    {
        private string m_MaKhachHang;

        public string MaKhachHang
        {
            get { return m_MaKhachHang; }
            set { m_MaKhachHang = value; }
        }
        private string m_HoTen;

        public string HoTen
        {
            get { return m_HoTen; }
            set { m_HoTen = value; }
        }
        private string m_DiaChi;

        public string DiaChi
        {
            get { return m_DiaChi; }
            set { m_DiaChi = value; }
        }
        private string m_SoDienThoai;

        public string SoDienThoai
        {
            get { return m_SoDienThoai; }
            set { m_SoDienThoai = value; }
        }
        private string m_Email;

        public string Email
        {
            get { return m_Email; }
            set { m_Email = value; }
        }
        private string m_NgayDangKy;

        public string NgayDangKy
        {
            get { return m_NgayDangKy; }
            set { m_NgayDangKy = value; }
        }
        public static List<TKKhachHang> getListKhachHang(List<PHIEUDANGKY> listOld, int thang, int nam)
        {
            List<TKKhachHang> listNew = new List<TKKhachHang>();
            List<string> listMaKH = new List<string>();
            try
            {
                foreach (PHIEUDANGKY d in listOld)
                {
                    DBEntities entities = new DBEntities();
                    TKKhachHang temp = new TKKhachHang();

                    DateTime thangnam = entities.PHIEUDANGKies.Where(t => t.MaPhieuDangKy == d.MaPhieuDangKy).SingleOrDefault().TuNgay;
                    if (thang == thangnam.Month && nam == thangnam.Year)
                    {
                        temp.MaKhachHang = d.MaKH.Trim();
                        temp.HoTen = entities.KHACHHANGs.Where(t => t.MaKH == d.MaKH).SingleOrDefault().HoTen;
                        temp.DiaChi = entities.KHACHHANGs.Where(t => t.MaKH == d.MaKH).SingleOrDefault().DiaChi;
                        temp.SoDienThoai = entities.KHACHHANGs.Where(t => t.MaKH == d.MaKH).SingleOrDefault().SoDT;
                        temp.Email = entities.KHACHHANGs.Where(t => t.MaKH == d.MaKH).SingleOrDefault().Email;
                        if (!listMaKH.Contains(temp.MaKhachHang.Trim()))
                        {
                            listMaKH.Add(temp.MaKhachHang.Trim());
                            listNew.Add(temp);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return listNew;
        }

        public static List<TKKhachHang> getListKHtheoDV(string idQC, ref float tyle)
        {
            List<TKKhachHang> listNew = new List<TKKhachHang>();
            List<string> listMaKH = new List<string>();
            int soluong = 0;
            int tong = 0;
            try
            {
                List<PHIEUDANGKY> que = DatabaseWrapper.GetInstance().GetAllPhieuDangKy();
                foreach (PHIEUDANGKY p in que)
                {
                    tong++;
                }
                switch (idQC)
                {
                    case "Tờ bướm":
                        List<TOBUOM> query = DatabaseWrapper.GetInstance().GetAllTOBUOM();
                        foreach (TOBUOM d in query)
                        {
                            DBEntities entities = new DBEntities();
                            TKKhachHang temp = new TKKhachHang();
                            string m_MaKH = entities.PHIEUDANGKies.Where(t1 => t1.MaPhieuDangKy == d.MaPhieuDangKy).SingleOrDefault().MaKH;
                            temp.MaKhachHang = m_MaKH.Trim();
                            temp.HoTen = entities.KHACHHANGs.Where(t => t.MaKH == m_MaKH).SingleOrDefault().HoTen;
                            temp.DiaChi = entities.KHACHHANGs.Where(t => t.MaKH == m_MaKH).SingleOrDefault().DiaChi;
                            temp.SoDienThoai = entities.KHACHHANGs.Where(t => t.MaKH == m_MaKH).SingleOrDefault().SoDT;
                            temp.Email = entities.KHACHHANGs.Where(t => t.MaKH == m_MaKH).SingleOrDefault().Email;
                            if (!listMaKH.Contains(temp.MaKhachHang.Trim()))
                            {
                                soluong++;
                                listMaKH.Add(temp.MaKhachHang.Trim());
                                listNew.Add(temp);
                            }
                            // tong++;

                        }
                        break;
                    case "Quảng cáo bảng":
                        List<QC_BANG> query1 = DatabaseWrapper.GetInstance().GetAllQCBANG();
                        foreach (QC_BANG d in query1)
                        {
                            DBEntities entities = new DBEntities();
                            TKKhachHang temp = new TKKhachHang();
                            string m_MaKH = entities.PHIEUDANGKies.Where(t1 => t1.MaPhieuDangKy == d.MaPhieuDangKy).SingleOrDefault().MaKH;
                            temp.MaKhachHang = m_MaKH.Trim();
                            temp.HoTen = entities.KHACHHANGs.Where(t => t.MaKH == m_MaKH).SingleOrDefault().HoTen;
                            temp.DiaChi = entities.KHACHHANGs.Where(t => t.MaKH == m_MaKH).SingleOrDefault().DiaChi;
                            temp.SoDienThoai = entities.KHACHHANGs.Where(t => t.MaKH == m_MaKH).SingleOrDefault().SoDT;
                            temp.Email = entities.KHACHHANGs.Where(t => t.MaKH == m_MaKH).SingleOrDefault().Email;
                            if (!listMaKH.Contains(temp.MaKhachHang.Trim()))
                            {

                                soluong++;
                                listMaKH.Add(temp.MaKhachHang.Trim());
                                listNew.Add(temp);
                            }
                            //tong++;
                        }
                        break;
                    case "Quảng cáo báo":
                        List<QC_BAO> query2 = DatabaseWrapper.GetInstance().GetAllQCBAO();
                        foreach (QC_BAO d in query2)
                        {
                            DBEntities entities = new DBEntities();
                            TKKhachHang temp = new TKKhachHang();
                            string m_MaKH = entities.PHIEUDANGKies.Where(t1 => t1.MaPhieuDangKy == d.MaPhieuDangKy).SingleOrDefault().MaKH;
                            temp.MaKhachHang = m_MaKH.Trim();
                            temp.HoTen = entities.KHACHHANGs.Where(t => t.MaKH == m_MaKH).SingleOrDefault().HoTen;
                            temp.DiaChi = entities.KHACHHANGs.Where(t => t.MaKH == m_MaKH).SingleOrDefault().DiaChi;
                            temp.SoDienThoai = entities.KHACHHANGs.Where(t => t.MaKH == m_MaKH).SingleOrDefault().SoDT;
                            temp.Email = entities.KHACHHANGs.Where(t => t.MaKH == m_MaKH).SingleOrDefault().Email;
                            if (!listMaKH.Contains(temp.MaKhachHang.Trim()))
                            {
                                soluong++;

                                listMaKH.Add(temp.MaKhachHang.Trim());
                                listNew.Add(temp);
                            }
                            // tong++;
                        }
                        break;
                    default:
                        break;
                }
                tyle = (float)soluong / (float)tong;
            }
            catch (Exception ex)
            {
            }
            
            return listNew;
        }
    }
}

